<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>侦听器</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
    <span>姓:</span>
    <input type="text" v-model="firstName"><br>
    <span>名:</span>
    <input type="text" v-model="lastName">
    <br>
    <span>{{fullName}}</span>

    <hr>
    <input type="text" v-model="A"><br>

    <input type="text" v-model="B">
    <br>
    <span>{{AB}}</span>
</div>
<div id="checkUser">
    <h3>验证用户名</h3>
    <input type="text" v-model.lazy="user">
    <span>{{tip}}</span>
</div>


<script>
    var app=new Vue({
        el:'#app',
        data:{
            firstName:'Tom',
            lastName:'jons',
            fullName:'Tom jons',

            A:'hello',
            B:'world'
        },
        watch:{
            firstName:function(val){
                this.fullName=val+' '+this.lastName;
            },
            lastName:function(val){
                this.fullName=this.firstName+' '+val;
            },

        },
        //使用计算属性来做和watch同样的效果
        computed:{
            AB:function () {
                return this.A+' '+this.B;
            }
        }
    })
    var checkUser=new Vue({
        el:'#checkUser',
        data:{
            user:'',
            tip:'',
        },
        watch:{
            user:function(val){
                this.tip='验证中..';
                this.check(val)
            },


        },
        methods:{
            check:function(val){
                let that=this;
                setTimeout(function () {
                    if(val=='admin' ||val=='hk'){
                        that.tip='已经存在，请更换';
                    }else {
                        that.tip='验证成功';
                    }

                },200)
            }
        }

    })

</script>
</body>
</html>